unit u_asus;

interface

uses
sysutils;

type

Tdiag = record
  numquest : int64;
  dial : string;
  balise_ouvre : string;
  balise_ferme : string;
  balise_exit : string;

end;

Ttab_diag = array of Tdiag;

Tscript = record
  tabdial : Ttab_diag;
  nom : string;
end;

function affichage(s : string;var myplay : boolean):string;
procedure traite_script(var dialog : Tscript; script : string);
function enlev_space(var s : string) : string;
function traite_tab(var dialog : Tscript;var  b : boolean; num :int64; userrep : integer;var myplay : boolean):string;
function reprise (var wait : boolean; text : string):string;

var
  konner : boolean;
  mars   : int64;
  dialoge : tscript; 
  obj : boolean;
  quetelance, inhit : boolean;
  but:string;
  type_mission:char;
  counter : integer;
  q_or_not : boolean;
  posxgpt,posygpt:integer;
implementation
uses
  u_player,u_engine,u_partic;



//givobj : pnj donne objet
//obj : joueur donne objet

function affichage(s : string;var myplay : boolean):string;
var
  l : string;
  begin

//    script := load_script('script.txt');
  //writeln(length(script));
  //lire(script);
  if dialoge.tabdial = nil  then
    traite_script(dialoge, s);

    if quetelance then
    begin
      mars:=mars *10 +1;
      counter:=1;
    end;
    if not(q_or_not) then
      begin
        obj:=false;
        quetelance:=false;
        inhit:= false;
        but := '';
        type_mission:=' ';
        konner := true;
        mars := 0;
        counter:=0;
        q_or_not:=false;
      end;
   result := traite_tab(dialoge,konner,mars,counter,myplay);

  end;




function enlev_space(var s : string) : string;
var
  i, pos : integer;
  good : string;
  begin
    i := 1;
    good := '';
    while i < length(s) do
      begin
        while (i < length(s)) and (s[i] <> '<') do
          i := i + 1;
        pos := i;
        if i > 1 then
          begin
            i := i - 1;
            if (s[i] = #10) or (s[1] = #9) or (s[i] = #32) then
              begin
                good := s.Substring(pos - 1, length(s) - pos + 1);
                while ((s[i] = #10 ) or (s[i] = #32) or (s[i] = #9)) and (i > 1)do
                  i := i - 1;
                if i <> 1 then
                  s := s.Substring(0, i) + good
                else s := good;
              end
            else pos := pos + 1;
          end
        else pos := pos + 1;
      i := pos;
      end;
    result := s;
  end;



procedure traite_script(var dialog : Tscript; script : string);
var
  i, case_dial, max : integer;
  num_quest : int64;
begin
    obj:=false;
    quetelance:=false;
    inhit:= false;
    but := '';
    type_mission:=' ';
    konner := true;
    mars := 0;
    counter:=0;
    q_or_not:=false;
  i := 1;
  while (i <= length(script)) and (script[i] <> '<') do
    begin
      dialog.nom := dialog.nom +  script[i];
      i := i + 1;
    end;
  if i <= length(script) then
    begin
      num_quest := 0;
      case_dial := 0;
      SetLength(dialog.tabdial,1);
      dialog.tabdial[case_dial].numquest := num_quest;
      while script[i] <> '>' do
        begin
          dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
          i := i + 1;
        end;
      dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
      i := i + 1;
      max := 0;
      num_quest := num_quest + 1;
      while i < Length(script) - 8 do
        begin
          if (case_dial = 0)
             and (script[i] = '<')
             and (script[i + 1] = 'r')
             and (dialog.tabdial[case_dial].balise_ouvre[2] <> 'r') then
            begin
              max := max + 1;
              case_dial := max;
              SetLength(dialog.tabdial,max + 1);
              dialog.tabdial[case_dial].numquest := num_quest;
              while script[i] <> '>' do
                begin
                  dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
                  i := i + 1;
                end;
              dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
              i := i + 1;
            end
          else
            if (script[i] = '<')
                and (script[i + 1] = 'r')
                and ((dialog.tabdial[case_dial].balise_ouvre[2] = 'r') or (dialog.tabdial[case_dial].balise_ferme = '</quest>')) then
              begin
                max := max + 1;
                case_dial := max;
                num_quest := num_quest + 1;
                SetLength(dialog.tabdial,max + 1);
                dialog.tabdial[case_dial].numquest := num_quest;
                while script[i] <> '>' do
                  begin
                    dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
                    i := i + 1;
                  end;
               dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
               i := i + 1;
              end
            else
              if (script[i] = '<')
                 and (script[i + 1] <> '/')
                 and (script[i + 1] <> 'e') then
                begin
                  max := max + 1;
                  case_dial := max;
                  num_quest := num_quest*10 + 1;
                  SetLength(dialog.tabdial,max + 1);
                  dialog.tabdial[case_dial].numquest := num_quest;
                  while script[i] <> '>' do
                    begin
                      dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
                      i := i + 1;
                    end;
                  dialog.tabdial[case_dial].balise_ouvre := dialog.tabdial[case_dial].balise_ouvre + script[i];
                  i := i + 1;
                end
              else
                if (script[i] <> '<') and (script[i] <> '>') then
                  while script[i] <> '<' do
                    begin
                      dialog.tabdial[case_dial].dial := dialog.tabdial[case_dial].dial + script[i];
                      i := i + 1;
                    end
                else
                  if (script[i] = '<') and (script[i + 1] = 'e') then
                    begin
                      while script[i] <> '>' do
                        begin
                          dialog.tabdial[case_dial].balise_exit := dialog.tabdial[case_dial].balise_exit + script[i];
                          i := i + 1;
                        end;
                      dialog.tabdial[case_dial].balise_exit := dialog.tabdial[case_dial].balise_exit + script[i];
                      i := i + 1;
                    end
                  else
                    if (script[i] = '<')
                        and (script[i + 1] = '/')
                        and (script[i + 2] = 'r')
                        and ((script[i + 7] = 'r') or (script[i + 7] = 'e')) then
                      begin
                        while script[i] <> '>' do
                          begin
                            dialog.tabdial[case_dial].balise_ferme := dialog.tabdial[case_dial].balise_ferme + script[i];
                            i := i + 1;
                          end;
                        dialog.tabdial[case_dial].balise_ferme := dialog.tabdial[case_dial].balise_ferme + script[i];
                        i := i + 1;
                      end
                    else
                      if (script[i] = '<') and (script[i + 1] = '/') and (num_quest >= 1) then
                        begin
                          num_quest := trunc(num_quest / 10);
                          while script[i] <> '>' do
                            begin
                              dialog.tabdial[case_dial].balise_ferme := dialog.tabdial[case_dial].balise_ferme + script[i];
                              i := i + 1;
                            end;
                          dialog.tabdial[case_dial].balise_ferme := dialog.tabdial[case_dial].balise_ferme + script[i];
                          i := i + 1;
                          while dialog.tabdial[case_dial].numquest <> num_quest do
                            case_dial := case_dial - 1;
                        end;
        end;
    end;
    case_dial := 0;
    dialog.tabdial[case_dial].balise_ferme := '</quest>';

end;



//dans l'implementation transformer en fonction et renvoyer si on donne, reçoit ou pas
//prevoir un parametre global de type booleen pour savoir si oui ou non je joueur a l'objet qu'il doit donner
//pour l'instant je me sert de find initialisé a false


function traite_tab(var dialog : Tscript;var  b : boolean; num :int64; userrep : integer;var myplay : boolean):string;
var
  i, long, k,j : integer;
  num_quest, temp : int64;
  find : boolean;
  s,temp2 : string;
  joie : tparticle;
begin
counter:=0;
temp2:='';

  long := length(dialog.tabdial);
  i:=0;
  dialoge := dialog;
  while (i<long)and (dialog.tabdial[i].numquest <> (num*10+userrep )) do
    begin
      i := i+1;
      myplay:=false;
    end;
      i:=i;
  num_quest:=num*10+userrep;
  while i<long do
    begin
      if dialog.tabdial[i].balise_ouvre = '<quest>' then
        begin
          if i = 0 then
            begin
              result := dialog.nom + ' dit:'+ dialog.tabdial[i].dial;
              i := i + 1;
              num_quest:=0;
            end
          else
            begin
              num_quest := num_quest  ;
              k := i;
              b:=true;
              result :=result + dialog.nom + ' dit:'+ dialog.tabdial[i].dial ;
              if dialog.tabdial[k].balise_exit = '<exit>' then
                exit
              else
                begin
                  while (k < long) and (dialog.tabdial[k].numquest <> num_quest*10 +1 ) do
                    k := k + 1;
                  i := k ;
                  num_quest := num_quest * 10+1;
                end;
            end;
        end
      else
        begin
          if dialog.tabdial[i].balise_ouvre = '<rep>' then
            begin
              if (b) then
                begin
                  k := i;
                  while k < long do
                    begin
                      if (dialog.tabdial[k].numquest div 10) = (num_quest div 10) then
                        begin
                          result:= result+#10 + inttostr(dialog.tabdial[k].numquest mod 10) + (') vous dites: ')+ (dialog.tabdial[k].dial);
                        //  num_quest := num_quest + 1;
                        end;
                    k := k + 1;
                    end;
                  num_quest:=num_quest div 10;
                  for j := 0 to counter-1 do
//                    num_quest:=num_quest div 10;
                  counter:=0;
                  k := i;
                  // demande a user de faire son choix
                  dialoge := dialog;
                  myplay := true;
                  mars := num_quest;
                  exit;
                end
              else
                begin
                  b:=true;
                  k := i;
                  if dialog.tabdial[k].balise_exit = '<exit>' then
                    exit
                  else
                    begin
                      if dialog.tabdial[k].dial='Accepter' then
                        begin
                          result:=result +'Accepter';
                          quetelance:=true;
                          i := k +1;
                          mars := num_quest div 10;
                          num_quest := num_quest * 10 + 1;
                          q_or_not:=true;
                          exit;
                        end;
                      i := k +1;
                      num_quest := num_quest * 10 + 1;
                    end;
                end;
            end
          else
            begin
              if dialog.tabdial[i].balise_ouvre = '<givobj>' then
                begin
                  k := i;
//                  num_quest := num_quest + 1;
                  if dialog.tabdial[k].dial = ' ' then
                    begin
                    result:=  dialog.nom + ' dit : resquilleur je te parle plus na';
                    exit;
                    end
                  else
                    begin
                      result := result  + (dialog.nom) + (' vous donne: ');
                      if (dialog.tabdial[k].dial[1] = 'a') and (dialog.tabdial[k].dial[2] = 'g') then
                        begin
                          temp := 3;
                          while temp <= Length(dialog.tabdial[k].dial) do
                            begin
                              result := result +(dialog.tabdial[k].dial[temp]);
                              temp2 := temp2 + (dialog.tabdial[k].dial[temp]);
                              temp := temp + 1;
                            end;
                        result := result +(' pièces d''or')+#10;
                        myplayer.tab_contenu[0].quantite := myplayer.tab_contenu[0].quantite + strtoint(temp2);
                        dialog.tabdial[k].dial:=' ';
                        num_quest := num_quest*10 +1;
                        i:=i+1;
                        end
                      else
                        begin
                          if dialog.tabdial[k].balise_exit = '<exit>' then
                            exit
                          else
                            begin
                              while dialog.tabdial[k].numquest <> num_quest * 10 + 1 do
                                k := k + 1;
                              i := k;
                              num_quest := num_quest * 10;
                            end;
                        end;
                    end;
                end
              else
                begin
                  if dialog.tabdial[i].balise_ouvre = '<obj>' then
                    begin
                      k := i;
                //      num_quest := num_quest*10 + 1;
                      if dialog.tabdial[k].dial = ' ' then
                        begin
                          result :=('objet déjà donné');
                          exit;
                        end
                      else
                        begin
                        if obj then
                          begin
                            if (dialog.tabdial[k].dial[1] = 'a') and (dialog.tabdial[k].dial[2] = 'g') then
                              begin
                                result := result + ('vous perdez ');
                                temp := 3;
                                while temp <= Length(dialog.tabdial[k].dial) do
                                  begin
                                    result := result + (dialog.tabdial[k].dial[temp]);
                                    temp := temp + 1;
                                  end;
                                result := result +(' pièces d''or');
                                dialog.tabdial[k].dial := ' ';
                                if dialog.tabdial[k].balise_exit = '<exit>' then
                                  exit
                                else
                                  begin
                                    while dialog.tabdial[k].numquest <> num_quest * 10 + 1 do
                                      k := k + 1;
                                    i := k;
                                    num_quest := num_quest * 10;
                                  end;
                              end
                          else
                            begin
                              result := (' quete terminer vous recever: ') + dialog.tabdial[k].dial + ' xp' + #10;
                              myplayer.carac.xp.actuel:=myplayer.carac.xp.actuel + strtoint(dialog.tabdial[k].dial);
                              while myplayer.carac.xp.stuff <= myplayer.carac.xp.actuel do
                                begin
                                  myplayer.carac.xp.actuel := myplayer.carac.xp.actuel - myplayer.carac.xp.stuff;
                                  myplayer.carac.niveau.actuel := myplayer.carac.niveau.actuel + 1;
                                  myplayer.carac.xp.stuff := trunc(1.5 * myplayer.carac.xp.stuff);
                                  myplayer.particle.add_part_lvlup(100,myplayer.get_x,myplayer.get_y,myplayer.get_x,myplayer.get_x);
                                  myplayer.carac.vie.actuel := (trunc(myplayer.carac.vie.stuff*1.3) - myplayer.carac.vie.stuff) + myplayer.carac.vie.actuel;
                                  myplayer.carac.vie.stuff := trunc(myplayer.carac.vie.stuff*1.3);
                                  q_or_not:=false;
                                  mymesh.map1[myplayer.etage,posxgpt,posygpt].script:='<quest>vous avez deja fait cette quete</quest>';
                                end;
                              dialog.tabdial[k].dial := ' ';
                              if dialog.tabdial[k].balise_exit = '<exit>' then
                                exit
                              else
                                begin
                                  while (k<long)and(dialog.tabdial[k].numquest <> num_quest * 10 + 1) do
                                    k := k + 1;
                                  i := k;
                                  obj:=false;
                                  num_quest := num_quest * 10+1;
                                end;
                            end;
                          end
                        else
                          begin
                          if (quetelance) then
                            begin
                              result := result +('vous n''avez pas encore l''objet') + #10;
                              konner:=true;
                              mars:=mars div 10;
                              myplayer.mychat.add_text('continuer ou arreter?');
                              myplayer.mychat.wait:=true;
                              exit;
                            end
                          else
                            begin
                              k:=1;
                              result := result + dialog.tabdial[i].dial ;
                              while (k<length(dialog.tabdial[i].dial))and(dialog.tabdial[i].dial[k]<>':') do
                                k:= k + 1;
                              k:= k+1;
                              for j := k to length(dialog.tabdial[i].dial) do
                                but := but +dialog.tabdial[i].dial[j];
                              k:=i;
                              type_mission :=  dialog.tabdial[i].dial[1];
                              inhit := true;
                              k := i;
                              while dialog.tabdial[k].numquest <> num_quest*10 +1  do
                                k := k + 1;
                              i := k  ;
                              counter:=counter+1;
                              num_quest := num_quest*10+1;
                              b:=true;
                            end;
                          end;
                        end;
//                      result := result + #10;
                    end;
                end;
            end;
        end;
    end;


if (i>long)and(b) then
  begin
    result:='apprend a taper';
    myplay:=true;
  end;
end;

function reprise (var wait : boolean; text : string):string;
var
  b:boolean;
begin
 konner:=false;
  if (wait)and(length(text)=1)and ((text[1])in['1'..'9']) then
    begin
      result:=traite_tab (dialoge, konner, mars, strtoint (text), wait);
    end
  else
    result:='';
end;

end.

